package com.singzi.leetcode.N1000;

import java.util.HashMap;

public class T0954 {
    public static void main(String[] args) {
        Solution0954 solution0954 = new Solution0954();
        int []arr = {2,4,0,0,8,1};
        System.out.println(solution0954.canReorderDoubled(arr));
    }
}

class Solution0954 {
    public boolean canReorderDoubled(int[] arr) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int zeros = 0;
        for(Integer num:arr){
            if(num == 0) zeros++;
            else if(num%2 == 0 && map.containsKey(num/2)){
                map.put(num/2, map.get(num/2)-1);
                if(map.get(num/2)==0) map.remove(num/2);
            }else if(map.containsKey(num*2)){
                map.put(num*2, map.get(num*2)-1);
                if(map.get(num*2)==0) map.remove(num*2);
            }
            else {
                map.put(num, map.getOrDefault(num, 0)+1);
            }
        }
        return map.size() == zeros;
    }
}